/*
 *  txdnet.cn tonydon
 * 
 */
package txdnet.util.coder;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import txdnet.util.Logger;
import txdnet.util.StaticMark;

/**
 * Hmac encrypt
 * @author txdnet
 */
public final class HMAC {

    public static String encryptMD5(String data, String secret){
        return encrypt(data, secret, "HmacMD5");
    }
    
    public static String encryptSHA1(String data, String secret){
        return encrypt(data, secret, "HmacSHA1");
    }
    
    public static String encrypt(String data, String secret, String algorithm) {
        byte[] bytes = null;
        try {
            SecretKey secretKey = new SecretKeySpec(secret.getBytes(StaticMark.ENCODING_UTF8), algorithm);
            Mac mac = Mac.getInstance(secretKey.getAlgorithm());
            mac.init(secretKey);
            bytes = mac.doFinal(data.getBytes(StaticMark.ENCODING_UTF8));
        } catch (Exception gse) {
            Logger.error(HMAC.class, gse);
        }
        return ByteHexUtil.byte2hex(bytes);
    }
    
//    public static void main(String... args){
//        System.out.println(HMAC.encryptMD5("123呵呵", "123"));
//        System.out.println(HMAC.encryptSHA1("123呵呵", "123"));
//    }
}
